package com.aiyige.page.my.order.dialog;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.page.my.order.model.KeyValue;
import com.aiyige.utils.widget.BaseBottomDialogFragment;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by zhuyakun on 2017/11/13.
 */

public class CommonOperationDialog extends BaseBottomDialogFragment {

    @BindView(R.id.ll_operation_content)
    LinearLayout linearLayout;

    @BindView(R.id.cancelBtn)
    TextView cancelBtn;
    Unbinder unbinder;

    List<KeyValue> list;


    public static CommonOperationDialog newInstance() {
        return new CommonOperationDialog();
    }

    Listener listener;

    public void setLisenter(Listener callback) {
        this.listener = callback;
    }

    public void setData(List<KeyValue> list) {
        this.list  = list;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_operation, container, false);
        unbinder = ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
        if (list == null || list.isEmpty()) {
            return;
        }
        for(int i=0;i<list.size();i++) {
            View item = LayoutInflater.from(getActivity()).inflate(R.layout.item_dialog_operation,null);
            TextView tvItem = (TextView)item.findViewById(R.id.item_operation);
            tvItem.setText(list.get(i).getValue());
            item.setTag(i);
            item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = -1;
                    try {
                        position = (int)v.getTag();
                        doSelected(list.get(position).getKey());
                    } catch (Exception e) {

                    }
                }
            });
            linearLayout.addView(item);
        }
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    private void doSelected(int position) {
        if (listener != null) {
            listener.onSelect(position);
        }
        dismiss();
    }


    public interface Listener {
        void onSelect(int position);
    }
}
